package com.cjd.common.recycler

import android.view.ViewPropertyAnimator
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/9/24
 * description recycler 淡入淡出
 */
class FadeItemAnimator : BaseItemAnimator() {
    /**
     * 执行移除动画
     *
     * @param holder   被移除的ViewHolder
     * @param animator 被移动的ViewHolder对应动画对象
     */
    override fun setRemoveAnimation(
        holder: RecyclerView.ViewHolder,
        animator: ViewPropertyAnimator
    ) {
        animator.alpha(0f)
    }

    /**
     * 执行移除动画结束，执行还原，因为该ViewHolder会被复用
     *
     * @param view 被移除的ViewHolder
     */
    override fun removeAnimationEnd(view: RecyclerView.ViewHolder) {
        ViewCompat.setAlpha(view.itemView, 1f)
    }

    /**
     * 执行添加动画初始化 这里设置透明为0添加时就会有渐变效果当然你可以在执行动画代码之前执行
     *
     * @param holder 添加的ViewHolder
     */
    override fun addAnimationInit(holder: RecyclerView.ViewHolder) {
        ViewCompat.setAlpha(holder.itemView, 0f)
    }

    /**
     * 执行添加动画
     *
     * @param holder   添加的ViewHolder
     * @param animator 添加的ViewHolder对应动画对象
     */
    override fun setAddAnimation(
        holder: RecyclerView.ViewHolder,
        animator: ViewPropertyAnimator
    ) {
        animator.alpha(1f)
    }

    /**
     * 取消添加还原状态以复用
     *
     * @param holder 添加的ViewHolder
     */
    override fun addAnimationCancel(holder: RecyclerView.ViewHolder) {
        ViewCompat.setAlpha(holder.itemView, 1f)
    }

    /**
     * 更新时旧的ViewHolder动画
     *
     * @param holder   旧的ViewHolder
     * @param animator ViewHolder对应动画对象
     */
    override fun setOldChangeAnimation(
        holder: RecyclerView.ViewHolder,
        animator: ViewPropertyAnimator
    ) {
        animator.alpha(0f)
    }

    /**
     * 更新时旧的ViewHolder动画结束，执行还原
     *
     * @param holder
     */
    override fun oldChangeAnimationEnd(holder: RecyclerView.ViewHolder) {
        ViewCompat.setAlpha(holder.itemView, 1f)
    }

    /**
     * 更新时新的ViewHolder初始化
     *
     * @param holder 更新时新的ViewHolder
     */
    override fun newChangeAnimationInit(holder: RecyclerView.ViewHolder) {
        ViewCompat.setAlpha(holder.itemView, 0f)
    }

    /**
     * 更新时新的ViewHolder动画
     *
     * @param holder   新的ViewHolder
     * @param animator ViewHolder对应动画对象
     */
    override fun setNewChangeAnimation(
        holder: RecyclerView.ViewHolder,
        animator: ViewPropertyAnimator
    ) {
        animator.alpha(1f)
    }

    /**
     * 更新时新的ViewHolder动画结束，执行还原
     *
     * @param holder
     */
    override fun newChangeAnimationEnd(holder: RecyclerView.ViewHolder) {
        ViewCompat.setAlpha(holder.itemView, 1f)
    }
}